package neu.soft.vendormock.service.impl;

import com.alibaba.fastjson.JSON;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import neu.soft.entity.FinancialPurchaseOrder;
import neu.soft.entity.Invoice;
import neu.soft.entity.InvoiceGoodInfo;
import neu.soft.service.InvoiceService;
import neu.soft.util.TypeConvert;
import neu.soft.vendormock.listener.MsgChannel;
import neu.soft.vendormock.service.SupplyMockService;
import neu.soft.vo.MsgBean;
import org.apache.rocketmq.common.message.MessageConst;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Service;
import org.springframework.util.MimeTypeUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ThreadLocalRandom;

/**
 * @author 李博
 * 模拟供应商供货
 */
@Service
@Slf4j
public class SupplyMockServiceImpl implements SupplyMockService {

    @Autowired
    InvoiceService invoiceService;

    @Autowired
    private MsgChannel msgChannel;

    private static final String[] namePool = {"张三", "李二", "王五", "一一", "诸葛七八", "赵四", "钱六", "关九", "周十", "吴十一", "杨十三", "孙十二", "丁真"};
    private static final String[] batchPool = {"001", "002", "003", "012", "040", "078", "098", "123", "234", "987", "146", "430", "039", "067"};

    private static Map<Integer, List<InvoiceGoodInfo>> goodInfoPool = new ConcurrentHashMap<>();

    private static final Map<String,String> identity = new HashMap<>();
    static {
        identity.put("桃李面包股份有限公司","91210100242588126L");
        identity.put("辽宁辉山乳业集团有限公司","91210100742732614M");
        identity.put("外星人食品有限公司","91350200MA8UH1XP94");
    }
    /**
     * map里有vendor->loc,购货单orderId,商品列表->发票商品列表
     */
    @SneakyThrows
    @Override
    public Map<String, Object> invoiceMock(Map<String, Object> map) {
        //得到商品信息列表，放入线程安全的map里
        String jsonObject = JSON.toJSONString(map.get("invoiceGoodInfoList"));
        List<InvoiceGoodInfo> invoiceGoodInfoList = TypeConvert.jsonArrayToList(jsonObject, InvoiceGoodInfo.class);
        //得到发票绑定的财务购货单id
        Integer financialId = Integer.valueOf((String) map.get("orderId"));
        //得到商品信息列表绑定的购货单id
        Integer operationId = Integer.valueOf((String) map.get("operationId"));
        goodInfoPool.put(operationId, invoiceGoodInfoList);
        //得到批次，使用ThreadLocalRandom随机数模拟
        int batchIndex = ThreadLocalRandom.current().nextInt(14);
        String batch = batchPool[batchIndex];
        map.put("batch", batch);
        //得到复核，收款人，开票人信息，随机数模拟
        int getMoneyOneIndex = ThreadLocalRandom.current().nextInt(14);
        int reviewIndex = ThreadLocalRandom.current().nextInt(14);
        int billingIndex = ThreadLocalRandom.current().nextInt(14);
        String getMoneyOne = namePool[getMoneyOneIndex];
        String review = namePool[reviewIndex];
        String billing = namePool[billingIndex];
        //设置发票相关的信息
        map.put("billing", billing);
        map.put("getMoneyOne", getMoneyOne);
        map.put("review", review);
        map.put("invoiceType", "-1");//登记时会覆盖
        map.put("note", " ");
        //模拟，生成发票
        Invoice invoice = invoiceService.buildInvoice(map);
        Map<String, Object> res = new HashMap<>();
        res.put("invoice", invoice);
        res.put("orderId", financialId);
        res.put("operationId",operationId);
        return res;
    }

    @SneakyThrows
    @Override
    public boolean sengGoods(Integer operationOrderId) {

        List<InvoiceGoodInfo> invoiceGoodInfoList = goodInfoPool.get(operationOrderId);
        MessageBuilder<MsgBean<List<InvoiceGoodInfo>>> builder = MessageBuilder.withPayload(new MsgBean<List<InvoiceGoodInfo>>(invoiceGoodInfoList, "发送货物-购货单id-" + operationOrderId))
                .setHeader(MessageConst.PROPERTY_DELAY_TIME_LEVEL, 0)
                .setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.APPLICATION_JSON);
        Message<MsgBean<List<InvoiceGoodInfo>>> message = builder.build();
        log.info("模拟供应商商品消息发送成功！等待消费");
        Thread.sleep(1000);
        return msgChannel.sendOutput().send(message);
    }
}
